/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.qsoft.general;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.http.HttpSession;
import org.apache.commons.codec.digest.DigestUtils;
//import oracle.toplink.essentials.ejb.cmp3.EntityManager;
//import oracle.toplink.essentials.sessions.DatabaseLogin;
//import org.apache.commons.codec.digest.DigestUtils;
//import org.ugc.cnel.manabi.entidades.TempUpload;
//import org.ugc.cnel.manabi.jpa.TempUploadJpaController;
//import org.ugc.cnel.manabi.jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author Admin
 */
public class Utilidades {

    //private static TempUploadJpaController jpatempupload = new TempUploadJpaController();
    private static EntityManagerFactory emf;

    public static String mayusculas(String name) {
        if (null != name && name.length() > 0) {
            return (name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase());
        } else {
            return name;
        }
    }

    public static String getSessionID() {
        FacesContext fCtx = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
        return session.getId();
    }

//    public static void eliminar_archivos_temp(String sesionId) {
//        List<TempUpload> lista = jpatempupload.getEntityManager().createNamedQuery("TempUpload.findBySessionId").setParameter("sessionId", sesionId).getResultList();
//        for (TempUpload tempUpload : lista) {
//            try {
//                File file = new File(tempUpload.getRuta());
//                if (file.exists()) {
//                    file.delete();
//                }
//                jpatempupload.destroy(tempUpload.getId());
//            } catch (NonexistentEntityException ex) {
//                Logger.getLogger(Utilidades.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        }
//    }

//    public static Connection conexion() {
//        emf = Persistence.createEntityManagerFactory("UGC_CNELPU");
//        oracle.toplink.essentials.ejb.cmp3.EntityManager entityManager = (EntityManager) emf.createEntityManager();
//        DatabaseLogin login = entityManager.getServerSession().getLogin();
//        try {
//            Connection conn = (Connection) login.connectToDatasource(null);
//            return conn;
//        } catch (Exception e) {
//            System.out.println(e);
//        }
//        return null;
//    }

    public static String md5(String cadena) {
        String md5 = DigestUtils.md5Hex(cadena);
        return md5;


    }
}
